package march;

import java.awt.BorderLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import quads.MarchQuad;
import save.Performance;
import save.PerformanceLoader;

public class GUI implements ActionListener {
	
	
	//private MarchableImage mi;
	//private Image quadPreview;
	private GridCanvas canvas;
	private Timekeeper keeper;
	
	
	public GUI () {
		
		final JFrame frame = new JFrame("Marching Squares");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		keeper = new Timekeeper();
		
		canvas = keeper.getGridCanvas();
		canvas.setBounds(0, 0, 800, 800);
		canvas.setFrame(frame);
		
		LayoutManager layout = new BorderLayout();
		
		frame.setLayout(layout);
		
		//frame.add(canvas, BorderLayout.CENTER);
		
		
		
		final JPanel panel = new JPanel();
		panel.setBounds(0, 0, 800, 800);
		frame.add(panel, BorderLayout.CENTER);
		panel.add(canvas);
		final JButton choose = new JButton("Choose Image");
		choose.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				int returnVal = chooser.showOpenDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File f = chooser.getSelectedFile();
					try {
						
						keeper.clearQuads();
						MarchableImage mi = new MarchableImage(f);
						
						
						canvas.setMarchableImage(mi);
						
						canvas.repaint();
						
						frame.pack();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}

			
		});
		
		
		final JButton imgToDeck = new JButton("Put Image on Deck");
		
		imgToDeck.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				int returnVal = chooser.showOpenDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File f = chooser.getSelectedFile();
					try {
						
						//keeper.clearQuads();
						MarchableImage mi = new MarchableImage(f, canvas.getCols());
						
						
						canvas.setImageOnDeck(mi);
						
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				
			}});
		
		final JButton transition = new JButton("Transition");
		
		transition.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				canvas.beginTransition();
				
			}
		});
		
		
		final JButton loadPerformance = new JButton("Load Performance");
		
		loadPerformance.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				JFileChooser chooser = new JFileChooser( new File("performances"));
				
				int returnVal = chooser.showOpenDialog(frame);
				
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					Performance p = PerformanceLoader.loadPerformance(chooser.getSelectedFile());
					Timekeeper.get().playPerformance(p);
				}
				
				
				
			}});
		
		JPanel quadChooser = createQuadChooser();
		
		frame.add(quadChooser, BorderLayout.EAST);
		
		JPanel btnPanel = new JPanel();
		
		frame.add(btnPanel, BorderLayout.NORTH);
		
		btnPanel.add(choose);
		btnPanel.add(imgToDeck);
		btnPanel.add(transition);
		btnPanel.add(loadPerformance);
		
		
		frame.pack();
		frame.setVisible(true);
		
	}
	

	
	
	private JPanel createQuadChooser() {
		
		JPanel jp = new JPanel();
		jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
		
		
	    ButtonGroup group = new ButtonGroup();
	    
	    
	    addQuadToChooser("Drum", "quads.DrumQuad", jp, group, true);
	    addQuadToChooser("Down", "quads.DownQuad", jp, group, false);
	    addQuadToChooser("Gran", "quads.GranQuad", jp, group, false);
	    addQuadToChooser("Loop", "quads.LoopQuad", jp, group, false);
	    
	    
	    setQuadOnDeck( group.getSelection().getActionCommand());
	   
	    
	    //quadPreview = new BufferedImage(80, 80, BufferedImage.TYPE_4BYTE_ABGR);
	    
	    return jp;
	    

	}
	
	
	private void addQuadToChooser(String label, String classname, JPanel jp, ButtonGroup group, boolean select) {
		
		
		
		JRadioButton drumButton = new JRadioButton(label);
	    drumButton.setActionCommand(classname);
	    drumButton.setSelected(select);

	   

	    

	    //Group the radio buttons.
	    group.add(drumButton);
	    

	    //Register a listener for the radio buttons.
	    drumButton.addActionListener(this);
	   
	    
	    jp.add(drumButton);
	    
	    
	}




	public static void main(String[] args) {
		
		@SuppressWarnings("unused")
		GUI g = new GUI();
		
	}




	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		
		String className = arg0.getActionCommand();
		
		setQuadOnDeck(className);
		
		
	}



	@SuppressWarnings("unchecked")
	private void setQuadOnDeck(String className) {
		Class<? extends MarchQuad> cls;
		try {
			cls =  (Class<? extends MarchQuad>) Class.forName(className);
			canvas.setQuadOnDeck(cls);
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
